#include <rtthread.h>
#include "base.h" 
#include "rtconfig.h"
#include "rtdef.h"

struct rt_semaphore park_sem; 

void car_entry_thread(void *parameter) {
    while (1) {
        // 想进停车场
        rt_sem_take(&park_sem, RT_WAITING_FOREVER);
        
        rt_kprintf("%s enter!\n", rt_thread_self()->name);
        rt_thread_mdelay(2000); // 停两秒
        rt_kprintf("%s exit!\n", rt_thread_self()->name);
        rt_sem_release(&park_sem);

        rt_thread_mdelay(1000);
    }
}

int main(void) {
    hardware_init();

    // 初始化“停车场”，有3个车位
    rt_sem_init(&park_sem, "park", 1, RT_IPC_FLAG_FIFO);

    // 创建多个模拟车辆的线程
    rt_thread_startup(rt_thread_create("car1", car_entry_thread, RT_NULL, 1024, 10, 20));
    rt_thread_startup(rt_thread_create("car2", car_entry_thread, RT_NULL, 1024, 10, 20));
    rt_thread_startup(rt_thread_create("car3", car_entry_thread, RT_NULL, 1024, 10, 20));
    rt_thread_startup(rt_thread_create("car4", car_entry_thread, RT_NULL, 1024, 10, 20));

    return 0;
}